<!DOCTYPE HTML>
<html>
<head>
    <title>秒杀商品详情</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="static/assets/global/plugins/bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="static/assets/global/css/components.css"/>
    <link rel="stylesheet" type="text/css" href="static/assets/admin/pages/css/login.css"/>
    <script type="text/javascript" src="static/assets/global/plugins/jquery-1.11.0.min.js"></script>
</head>
<body class="login">
<div class="content">
    <h3 class="form-title">秒杀商品详情</h3>
    <label style="color: blue;" id="titleContent" class="control-label"></label>
    <div id="promoStartDateContainer" class="form-group">
        <label style="color: blue;" id="promoStatus" class="control-label"></label>
        <div>
            <label style="color: red;" class="control-label" id="promoStartDate"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">商品名</label>
        <div>
            <label class="control-label" id="itemname"/>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label">商品描述</label>
        <div>
            <label class="control-label" id="description"/>
        </div>
    </div>

    <div id="normalPrice" class="form-group">
        <label class="control-label">价格</label>
        <div>
            <label class="control-label" id="price"/>
        </div>
    </div>
    <div id="promoPriceContainer" class="form-group">
        <label style="color: red;" class="control-label">秒杀价格</label>
        <div>
            <label style="color: red;" class="control-label" id="promoPrice"/>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label">图片</label>
        <div>
            <img id="imgurl" style="width:200px;height: auto;">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label">库存</label>
        <div>
            <label class="control-label" id="stock"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">销量</label>
        <div>
            <label class="control-label" id="sales"/>
        </div>
    </div>
    <div class="form-actions">
        <button class="btn blue" id="createOrder" type="submit">提交订单</button>
    </div>
</div>
</body>
<script type="text/javascript">

    var g_itemVo = {};

    jQuery(document).ready(function () {

        //根据URL传参获取参数值（get请求）
        function getParam(paramName) {
            paramValue = "", isFound = !1;
            if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
                arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
                while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
            }
            return paramValue == "" && (paramValue = null), paramValue
        }

        //获取商品详情接口数据
        $.ajax({
            type: "GET",
            url: "http://localhost:8081/miaosha/item/getItemInfo",
            data: {
                "id": getParam("id")
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.status == "success") {
                    // window.location.href="login.html"
                    g_itemVo = data.data;
                    console.log(g_itemVo.title);
                    reloadDom();
                } else {
                    alert("获取商品详情失败：" + data.data.errMsg);
                }
            },
            error: function (data) {
                alert("获取商品详情失败：" + data.responseText);
            }
        });
    });

    //获取数据模型之后填充数据
    function reloadDom() {
        var promoStatus = g_itemVo.promoStatus;
        console.log("g_itemVo.promoStatus->" + g_itemVo.promoStatus);
        if ( promoStatus== 0) {
            $("#titleContent").text("没有秒杀活动");
        } else if (promoStatus == 1) {
            $("#titleContent").text("秒杀活动尚未开始");
        } else if (promoStatus == 2) {
            $("#titleContent").text("秒杀活动进行中");
        }
        $("#itemname").text(g_itemVo.title);
        $("#description").text(g_itemVo.description);
        $("#price").text(g_itemVo.price);
        $("#imgurl").attr("src", g_itemVo.imgUrl);
        $("#stock").text(g_itemVo.stock);
        $("#sales").text(g_itemVo.sales);
        if (promoStatus == 1) {
            //秒杀活动尚未开始
            console.log("g_itemVo.startTime->"+g_itemVo.startTime);
            var startTime = g_itemVo.startTime.replace(new RegExp("-", "gm"), "/");
            console.log("startTime-1->"+startTime);
            startTime = (new Date(startTime)).getTime();
            console.log("startTime-2->"+startTime);
            var nowTime = Date.parse(new Date());
            console.log("nowTime->"+nowTime);
            var delta = (startTime - nowTime) / 1000;
            console.log("delta->"+delta);
            if (delta < 0) {
                g_itemVo.promoStatus = 2;
                reloadDom();
            }
            $("#promoStartDate").text("秒杀活动的开始时间：" + g_itemVo.startTime + "，倒计时：" + delta + "秒");
            $("#promoPrice").text(g_itemVo.promoPrice);
            $("#createOrder").attr("disabled", true);
        } else if (promoStatus == 2) {
            //秒杀活动进行中
            $("#promoStartDate").text("秒杀活动进行中");
            $("#promoPrice").text(g_itemVo.promoPrice);
            $("#createOrder").attr("disabled", false);
            $("#normalPrice").hide();
        } else if (promoStatus == 0) {//没有秒杀活动
            $("#promoPriceContainer").hide();
            $("#promoStartDateContainer").hide();
        }
    }

    //创建订单
    $("#createOrder").on("click", function () {
        $.ajax({
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            url: "http://localhost:8081/miaosha/order/createOrder",
            data: {
                "itemId": g_itemVo.id,
                "amount": 1,
                "promoId": g_itemVo.promoId
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.status == "success") {
                    alert("下单成功");
                    window.location.reload();
                } else {
                    alert("下单失败：" + data.data.errMsg);
                    if (data.data.errCode == 200004) {
                        window.location.href = "login.html";
                    }
                }
            },
            error: function (data) {
                alert("下单失败：" + data.responseText);
            }
        });
    });
</script>
</html>